package com.systemic.service.impl;

import com.systemic.service.FriendApplyService;
import com.systemic.mapper.FriendApplyMapper;
import com.systemic.mapper.FriendMapper;
import com.systemic.mapper.UserInfoMapper;
import com.systemic.pojo.Friend;
import com.systemic.pojo.FriendApply;
import com.systemic.pojo.UserInfo;
import com.systemic.vo.ReceiveApply;
import com.systemic.vo.UserItem;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: FriendApplyServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2023-12-29 10:28
 * @Version:
 **/
@Service
public class FriendApplyServiceImpl implements FriendApplyService {

    @Resource
    private FriendApplyMapper friendApplyMapper;

    @Resource
    private FriendMapper friendMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 好友申请业务处理
     * @param friendApply
     */
    @Override
    @Transactional
    public void insert(FriendApply friendApply) {
        if (friendApply!=null){
            // 查询目标用户是否是当前用户的好友
            FriendApply friend = friendApplyMapper.select(friendApply);
            if (friend==null){
                friendApply.setStatus(1);
                friendApply.setApplyTime(new Date());
                friendApplyMapper.insert(friendApply);
            } else if (friend.getStatus()==1){
                throw new RuntimeException("已发送过申请，请耐心等待确认");
            } else if (friend.getStatus()==2){
                throw new RuntimeException("你们已经成为好友");
            } else {
                throw new RuntimeException("未知错误");
            }
        } else {
            throw new RuntimeException("好友申请不能为空");
        }

    }

    @Override
    public FriendApply select(FriendApply friend) {
        return friendApplyMapper.select(friend);
    }

    /**
     * 查看好友申请
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public List<ReceiveApply> selectListById(Integer uid) {
        List<FriendApply> friendApplies = friendApplyMapper.selectListByTarget(uid);
        if (friendApplies!=null){
            List<ReceiveApply> receiveApplies = new ArrayList<>();
            for (FriendApply friendApply: friendApplies) {
                ReceiveApply receiveApply = new ReceiveApply();
                try{
                    if (friendApply.getStatus()==1){
                        Integer fid = friendApply.getUid();
                        // status=1表示申请中，因为此业务就是要查看有哪些用户对当前用户发起好友申请
                        receiveApply.setUid(friendApply.getTarget());
                        receiveApply.setFid(fid);
                        // 根据fid查询头像和用户名
                        UserItem item = userInfoMapper.selectEntityById(fid);
                        if (item != null){
                            receiveApply.setNickname(item.getUname());
                            receiveApply.setAvatar(item.getAvatar());
                        }
                        receiveApply.setApplyInfo(friendApply.getApplyInfo());
                        receiveApply.setApplyTime(friendApply.getApplyTime());
                        receiveApplies.add(receiveApply);
                    }
                }catch (Exception e){
                    throw new RuntimeException("出现错误"+e);
                }
            }
            return receiveApplies;
        }
        return null;
    }


    /**
     * 同意好友申请，建立双向好友关系
     * @param friendApply
     * @return
     */
    @Override
    @Transactional
    public int updateFriendApply(FriendApply friendApply) {
        FriendApply apply = new FriendApply();
        if (friendApply!=null){
            int user1 = friendApply.getTarget();
            int user2 = friendApply.getUid();
            try{
                // 根据id查询信息
                UserInfo userInfo1 = userInfoMapper.selectUserInfoByUid(user1);
                UserInfo userInfo2 = userInfoMapper.selectUserInfoByUid(user2);
                // 获取昵称
                String name1 = userInfo1.getUname();
                String name2 = userInfo2.getUname();
                // 建立双向新增（让双方彼此出现在各自的好友列表里）
                Friend friend1 = new Friend();
                Friend friend2 = new Friend();

                friend1.setUid(user1);
                friend1.setFid(user2);
                friend1.setNickname(name2);

                friend2.setUid(user2);
                friend2.setFid(user1);
                friend2.setNickname(name1);

                friendMapper.insert(friend1);
                friendMapper.insert(friend2);

            }catch (Exception e){
                throw new RuntimeException("出现错误" + e);
            }
            apply.setUid(user1);
            apply.setTarget(user2);
            apply.setStatus(2);
        }
        int rows = friendApplyMapper.updateFriendApply(apply);
        if (rows==0){
            throw new RuntimeException("失败");
        }
       return rows;
    }


    /**
     * 忽略好友申请
     * @param friendApply
     * @return
     */
    @Override
    @Transactional
    public int ignoreFriendApply(FriendApply friendApply) {
        int rows=0;
        if (friendApply!=null){
            FriendApply apply = new FriendApply();
            apply.setUid(friendApply.getTarget());
            apply.setTarget(friendApply.getUid());
            apply.setStatus(friendApply.getStatus());
            rows = friendApplyMapper.updateFriendApply(apply);
        }
        if (rows==0){
            throw new RuntimeException("失败");
        }
        return rows;
    }


}
